//
//  VisitWebViewController.m
//  meieshidai
//
//  Created by longziyuan on 14-3-18.
//
//

#import "VisitWebViewController.h"
#import "iToast.h"
#import "GlobalVar.h"
@interface VisitWebViewController ()

@end

@implementation VisitWebViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
    }
    self.title=[[NSUserDefaults standardUserDefaults] objectForKey:@"vipName"];
    UIBarButtonItem* backButton=[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem=backButton;
    [backButton release];
    UIBarButtonItem* refreshButton=[[UIBarButtonItem alloc]initWithTitle:@"刷新" style:UIBarButtonItemStyleDone target:self action:@selector(refreshAction:)];
    self.navigationItem.rightBarButtonItem=refreshButton;
    [refreshButton release];
    webView=[[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)]autorelease];
    [webView setFrame:CGRectMake(0, 0, IPHONE_W, IPHONE_H-49-44-20)];
    webView.delegate=self;
    webView.scalesPageToFit=YES;
    [self.view addSubview:webView];
}
-(void)viewWillAppear:(BOOL)animated
{
    NSURL* url=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:@"vipUrl"]];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];;
    activity= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    activity.center =CGPointMake(self.view.center.x, self.view.center.y-50);
    [self.view addSubview:activity];
    activity.color = [UIColor grayColor];
    [super viewWillAppear:animated];
}
-(void)backAction:(id)sender
{
    if ([webView canGoBack])
    {
        [webView goBack];
    }
}

-(void)refreshAction:(id)sender
{
    if (webView.isLoading)
    {
        [webView stopLoading];
    }
    [webView reload];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    if (!activity.isAnimating)
    {
        [self.view addSubview:activity];
        [activity startAnimating];
    }
    //NSLog(@"=====startAnimating=====");
    //HUD=[[[MBProgressHUD alloc]initWithView:self.view]autorelease];
    // HUD.removeFromSuperViewOnHide=YES;
    // [self.view addSubview:HUD];
    // [HUD setLabelFont:[UIFont systemFontOfSize:12]];
    // [HUD setLabelText:@"正在加载网页数据..."];
    // [HUD show:YES];
}
/*
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}
*/
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //NSLog(@"===webViewDidFinishLoad===");
    //[HUD hide:YES];
    if (activity.isAnimating)
    {
        [activity stopAnimating];
        [activity removeFromSuperview];
    }
}

-(void)viewDidDisappear:(BOOL)animated
{
    if (activity.isAnimating)
    {
        [activity stopAnimating];
        [activity removeFromSuperview];
        [HUD removeFromSuperview];
    }
    [super viewDidDisappear:animated];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"error===%@",error.debugDescription);
    if (error.code!=NSURLErrorCancelled)
    {
        //iToast* toast=[iToast makeText:@"网络链接超时"];
        //[toast show];
        HUD=[[[MBProgressHUD alloc]initWithView:self.view]autorelease];
        HUD.removeFromSuperViewOnHide=YES;
        [self.view addSubview:HUD];
        [HUD setLabelFont:[UIFont systemFontOfSize:12]];
        [HUD setLabelText:@"网络异常"];
        [HUD show:YES];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
